<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Docker学习笔记 | 个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Docker学习笔记当人们说“Docker”时，他们通常是指 Docker Engine，它是一个客户端 - 服务器应用程序，由 Docker守护进程、一个REST API指定与守护进程交互的接口、和一个命令行接口（CLI）与守护进程通信（通过封装REST API）。Docker Engine 从 CLI 中接受docker 命令，例如 docker run 、docker ps 来列出正在运行的">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker学习笔记">
<meta property="og:url" content="http://codercheng.cn/2021/03/18/Docker%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="Docker学习笔记当人们说“Docker”时，他们通常是指 Docker Engine，它是一个客户端 - 服务器应用程序，由 Docker守护进程、一个REST API指定与守护进程交互的接口、和一个命令行接口（CLI）与守护进程通信（通过封装REST API）。Docker Engine 从 CLI 中接受docker 命令，例如 docker run 、docker ps 来列出正在运行的">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/150851-563512.png">
<meta property="og:image" content="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/161605-357589.png">
<meta property="og:image" content="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/151858-535527.png">
<meta property="og:image" content="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/153201-770904.png">
<meta property="article:published_time" content="2021-03-18T07:33:27.080Z">
<meta property="article:modified_time" content="2021-03-18T08:17:33.560Z">
<meta property="article:author" content="CoderCheng">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/150851-563512.png">
  
    <link rel="alternate" href="/atom.xml" title="个人博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.lug.ustc.edu.cn/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://codercheng.cn"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Docker学习笔记" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/03/18/Docker%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2021-03-18T07:33:27.080Z" itemprop="datePublished">2021-03-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Docker学习笔记
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <pre><code>Docker学习笔记</code></pre><p>当人们说“Docker”时，他们通常是指 Docker Engine，它是一个客户端 - 服务器应用程序，由 Docker守护进程、一个REST API指定与守护进程交互的接口、和一个命令行接口（CLI）与守护进程通信（通过封装REST API）。Docker Engine 从 CLI 中接受docker 命令，例如 docker run 、docker ps 来列出正在运行的容器、docker images 来列出镜像，等等。</p>
<p><strong>docker</strong> <strong>基本组成</strong></p>
<p>docker主机(Host)：安装了Docker程序的机器（Docker直接安装在操作系统之上）；</p>
<p>docker仓库(Registry)：用来保存各种打包好的软件镜像；仓库分为公有仓库和私有仓库。(很类似maven)</p>
<p>docker镜像(Images)：软件打包好的镜像；放在docker仓库中；</p>
<p>docker容器(Container)：镜像启动后的实例称为一个容器；容器是独立运行的一个或一组应用</p>
<p><strong>docker 与操作系统比较</strong></p>
<p>docker是一种轻量级的虚拟化方式</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/150851-563512.png" alt="image-20210318150849177"></p>
<p> Docker常用命令</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/161605-357589.png" alt="image-20210318151000222"></p>
<p><strong>images命令</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker images </span><br><span class="line">docker image ls</span><br></pre></td></tr></table></figure>

<p><strong>save命令</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p /data </span><br><span class="line">cd /data </span><br><span class="line">docker save tomcat:9.0.20-jre8-alpine -o tomcat9.tar </span><br><span class="line">docker save tomcat:9.0.20-jre8-slim &gt; tomcat9.slim.tar</span><br></pre></td></tr></table></figure>

<p><strong>inspect命令</strong></p>
<ul>
<li>通过 docker inspect 命令，我们可以获取镜像的详细信息，其中，包括创建者，各层的数字摘要等。</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker inspect tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span></span><br><span class="line">docker inspect <span class="operator">-f</span> &#123;&#123;<span class="string">".Size"</span>&#125;&#125; tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span></span><br></pre></td></tr></table></figure>

<p><strong>history命令</strong></p>
<ul>
<li>从前面的命令中，我们了解到，一个镜像是由多个层组成的，那么，我们要如何知道各个层的具体内容呢？</li>
</ul>
<p>通过 docker history命令，可以列出各个层的创建信息，例如：查看 tomcat:9.0.20-jre8-alpine的各层信息</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker history tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span></span><br></pre></td></tr></table></figure>

<p><strong>tag命令</strong></p>
<ul>
<li>标记本地镜像，将其归入某一仓库</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker tag tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span> lagou/tomcat:<span class="number">9</span></span><br></pre></td></tr></table></figure>

<p><strong>rmi命令</strong></p>
<p>  <strong>常用参数</strong></p>
<ul>
<li>-f, -force : 强制删除镜像，即便有容器引用该镜像；</li>
<li>-no-prune : 不要删除未带标签的父镜像；</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker rmi tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span> </span><br><span class="line">docker image rm tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span></span><br></pre></td></tr></table></figure>

<p><strong>清理镜像</strong></p>
<p>我们在使用 Docker 一段时间后，系统一般都会残存一些临时的、没有被使用的镜像文件，可以通过以下命令进行清理。执行完命令后，还是告诉我们释放了多少存储空间！</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker image prune</span><br></pre></td></tr></table></figure>

<p>​                                                                             <strong>Docker容器（container）</strong></p>
<p><strong>容器是镜像的运行时实例</strong></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/151858-535527.png" alt="image-20210318151857629"></p>
<p><strong>新建并启动容器</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run [<span class="type">OPTIONS</span>] IMAGE [<span class="type">COMMAND</span>] [<span class="type">ARG...</span>]</span><br></pre></td></tr></table></figure>

<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run <span class="literal">-it</span> -<span class="literal">-rm</span> <span class="literal">-p</span> <span class="number">8080</span>:<span class="number">8080</span> tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span></span><br></pre></td></tr></table></figure>

<p><strong>docker run</strong>命令常用参数比较多，这里仅仅列出开发岗常用参数，请小伙伴们自行查找资料获得更多参数信息</p>
<p><strong>-d, –detach=false:</strong> 后台运行容器，并返回容器ID</p>
<p><strong>-i, –interactive=false:</strong> 以交互模式运行容器，通常与 -t 同时使用</p>
<p><strong>-P, –publish-all=false:</strong> 随机端口映射，容器内部端口随机映射到主机的端口。<strong>不推荐各位小伙伴使用该参数</strong></p>
<p><strong>-p, –publish=[]:</strong> 指定端口映射，格式为：主机(宿主)端口容器端口，推荐各位小伙伴们使用**</p>
<p><strong>-t, –tty=false:</strong> 为容器重新分配一个伪输入终端，通常与 -i 同时使用</p>
<p><strong>–name=”nginx-lb”:</strong> 为容器指定一个名称</p>
<p><strong>-h , –hostname=”laosiji”:</strong> 指定容器的hostname</p>
<p><strong>-e , –env=[]:</strong> 设置环境变量，容器中可以使用该环境变量</p>
<p><strong>–net=”bridge”:</strong> 指定容器的网络连接类型，支持 bridge/host/none/container: 四种类型</p>
<p><strong>–link=[]:</strong> 添加链接到另一个容器；<strong>不推荐各位小伙伴使用该参数</strong></p>
<p><strong>-v, –volume :</strong> 绑定一个卷</p>
<p><strong>–privileged=false:</strong> 指定容器是否为特权容器，特权容器拥有所有的capabilities </p>
<p><strong>–restart=no</strong>：指定容器停止后的重启策略</p>
<p>no：容器退出时不重启 </p>
<p>on-failure：容器故障退出（返回值非零）时重启 </p>
<p>always：容器退出时总是重启,<strong>推荐各位小伙伴们使用</strong> </p>
<p><strong>–rm=false:</strong> 指定容器停止后自动删除容器,<strong>不能以</strong>docker run -d<strong>启动的容器</strong></p>
<p><strong>容器日志</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker logs [OPTIONS] CONTAINER</span><br></pre></td></tr></table></figure>

<p><strong>-f :</strong> 跟踪日志输出</p>
<p><strong>–tail :</strong>仅列出最新N条容器日志</p>
<p><strong>删除容器</strong></p>
<p><strong>docker rm</strong> <strong>：</strong>删除一个或多个容器。docker rm命令只能删除处于终止或退出状态的容器，并不能删除还处于运行状态容器</p>
<p><strong>-f :</strong>通过 SIGKILL 信号强制删除一个运行中的容器。</p>
<p><strong>-l :</strong>移除容器间的网络连接，而非容器本身。</p>
<p><strong>-v :</strong>删除与容器关联的卷。</p>
<p><strong>列出容器</strong></p>
<p>docker ps [OPTIONS] </p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker run <span class="literal">-itd</span> -<span class="literal">-name</span> tomcat9 <span class="literal">-p</span> <span class="number">8080</span>:<span class="number">8080</span> tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span> </span><br><span class="line">查看运行中的容器 </span><br><span class="line">docker ps tomcat9 </span><br><span class="line">查看所有容器 </span><br><span class="line">docker ps <span class="literal">-a</span> tomcat9</span><br></pre></td></tr></table></figure>

<p><strong>启动、重启、终止容器</strong></p>
<p><strong>docker start</strong> :启动一个或多个已经被停止的容器</p>
<p><strong>docker stop</strong> :停止一个运行中的容器</p>
<p><strong>docker restart</strong> :重启容器</p>
<p><strong>进入容器</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker exec [OPTIONS] CONTAINER COMMAND [ARG...]</span><br></pre></td></tr></table></figure>

<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">有bash命令的linux系统：例如centos</span><br><span class="line">docker run <span class="literal">-it</span> -<span class="literal">-name</span> tomcat9.<span class="number">1</span> <span class="literal">-p</span> <span class="number">8080</span>:<span class="number">8080</span> tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-slim</span></span><br><span class="line">docker exec <span class="literal">-it</span> tomcat9.<span class="number">1</span> /bin/bash </span><br><span class="line">没有bash命令的linux系统：例如alpine系统 </span><br><span class="line">docker run <span class="literal">-it</span> -<span class="literal">-name</span> tomcat9.<span class="number">2</span> <span class="literal">-p</span> <span class="number">8081</span>:<span class="number">8080</span> tomcat:<span class="number">9.0</span>.<span class="number">20</span><span class="literal">-jre8</span><span class="literal">-alpine</span> </span><br><span class="line">docker exec <span class="literal">-it</span> tomcat9.<span class="number">2</span> sh</span><br></pre></td></tr></table></figure>

<p><strong>查看容器</strong></p>
<p><strong>docker inspect :</strong> 获取容器/镜像的元数据。</p>
<p> 语法 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker inspect [OPTIONS] NAME|ID [NAME|ID...]</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker run -it --name tomcat9 -p 8081:8080 tomcat:9.0.20-jre8-alpine </span><br><span class="line">docker inspect tomcat9</span><br></pre></td></tr></table></figure>

<p>常用参数</p>
<p><strong>-f :</strong>指定返回值的模板文件。</p>
<p><strong>-s :</strong>显示总的文件大小。</p>
<p><strong>–type :</strong>为指定类型返回JSON。</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202103/18/153201-770904.png" alt="image-20210318153201331"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2021/03/18/Docker%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-id="cko3snvw80002i8uo2t6d3cvp" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/03/31/k8s/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          k8s
        
      </div>
    </a>
  
  
    <a href="/2021/03/11/ElasticSearch%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">ElasticSearch学习笔记</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/MySql/">MySql</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Netty/">Netty</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringBoot/">SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringCloud/">SpringCloud</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringMVC/">SpringMVC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Tomcat/">Tomcat</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Zookeeper/">Zookeeper</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E4%BD%9C%E4%B8%9A/">作业</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E8%B0%83%E5%BA%A6/">分布式调度</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%A1%86%E6%9E%B6/">框架</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%9F%A5%E8%AF%86/">知识</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2024/08/">八月 2024</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/09/">九月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/08/">八月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/04/">四月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">二月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/11/">十一月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">六月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/02/">二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">一月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">十二月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">十一月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/10/">十月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/09/">九月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">八月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2024/08/28/CentOS%E5%86%85%E5%AD%98cpu%E6%8E%92%E6%9F%A5/">CentOS内存cpu排查</a>
          </li>
        
          <li>
            <a href="/2022/09/22/Linux%20%E6%9F%A5%E7%9C%8B%E6%97%A5%E5%BF%97%E5%91%BD%E4%BB%A4/">Linux 查看日志命令</a>
          </li>
        
          <li>
            <a href="/2022/08/30/dubbo%20%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/">dubbo 常见问题</a>
          </li>
        
          <li>
            <a href="/2022/04/26/1%20%E6%BA%90%E7%A0%81%E7%AC%94%E8%AE%B0/">1 源码笔记</a>
          </li>
        
          <li>
            <a href="/2022/02/24/jvisualVM%20%E4%BD%BF%E7%94%A8%E7%AC%94%E8%AE%B0/">jvisualVM 使用笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2024 CoderCheng<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//code.jquery.com/jquery-2.0.3.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>